<?php
class article
{
	public static function find_all($limit=20,$offset=0)
	{
		$article_path = Kohana::config('core.article_data_path');
		$files = self::filter_meta_files();
		$meta_arr = array();
		if($files_num = count($files))
		{
			if($files_num<$offset)
				return array();
			$count = $files_num>$limit*($offset+1)?$limit*($offset+1):$files_num;
			for($i=$offset;$i<$count;$i++)
			{
				$meta_info = explode("|",file_get_contents($files[$i]));
				$meta_arr[$i]['id'] = $meta_info[0];
				$meta_arr[$i]['email'] = $meta_info[1];
				$meta_arr[$i]['type'] = $meta_info[2];
				$meta_arr[$i]['status'] = $meta_info[3];
				$meta_arr[$i]['vote'] = $meta_info[4];
				$meta_arr[$i]['pubdate'] = $meta_info[5];
				$meta_arr[$i]['title'] = $meta_info[6];
			}
			$meta_arr = array_slice($meta_arr,0);
		}
		return $meta_arr;
	}

	public static function count_all()
	{
		return count(self::filter_meta_files());
	}

	public function filter_meta_files()
	{
		$article_path = Kohana::config('core.article_data_path');
		($files = @glob($article_path."*.meta")) OR $files = array();
		if(isset($_GET['type']))
		{
			$type = (int)$_GET['type'];
			$count = count($files);
			for($i=0;$i<$count;$i++)
			{
				$meta = explode('|',file_get_contents($files[$i]));
				if($meta[2] != $type)
				{
					unset($files[$i]);
				}
			}
		}
		elseif(isset($_GET['status']))
		{
			$type = (int)$_GET['status'];
			$count = count($files);
			for($i=0;$i<$count;$i++)
			{
				$meta = explode('|',file_get_contents($files[$i]));
				if($meta[3] != $type)
				{
					unset($files[$i]);
				}
			}
		}
		elseif(isset($_GET['vote']))
		{
			usort($files,array('article','_vote_sort'));
		}
		else
		{
			natsort($files);
			$files = array_reverse($files);
		}
		return array_slice($files,0);
	}

	private static function _vote_sort($a,$b)
	{
		$meta_a = explode('|',file_get_contents($a));
		$meta_b = explode('|',file_get_contents($b));
		if($meta_a[4] == $meta_b[4]) return 0;
		return $meta_a[4] > $meta_b[4]?-1:1;
	}

	public static function find($article_id)
	{
		$article_id = (int)$article_id;
		$article_path = Kohana::config('core.article_data_path');
		$content = @file_get_contents($article_path.$article_id.'.content');
		if(!$content) return false;
		$meta = explode('|',file_get_contents($article_path.$article_id.'.meta'));
		$meta_info = array('id','email','type','status','vote','pubdate','title');
		$meta = array_combine($meta_info,$meta);
		$comments = comment::get_all_by_article_id($article_id);
		return array('meta'=>$meta,'content'=>$content,'comments'=>$comments);
	}

	public static function get_meta($article_id)
	{
		$article_id = (int)$article_id;
		$article_path = Kohana::config('core.article_data_path');
		$content = @file_get_contents($article_path.$article_id.'.meta');
		if(!$content) return false;
		$meta = explode('|',$content);
		$meta_info = array('id','email','type','status','vote','pubdate','title');
		return array_combine($meta_info,$meta);
	}

	public static function get_content($article_id)
	{
		$article_id = (int)$article_id;
		$article_path = Kohana::config('core.article_data_path');
		return @file_get_contents($article_path.$article_id.'.content');
	}

	public static function update_content($article_id,$content)
	{
		$article_path = Kohana::config('core.article_data_path').$article_id.'.content';
		file_put_contents($article_path,$content);
	}
	
	public static function update_meta($article_id,$info = array())
	{
		$article_id = (int)$article_id;
		$article_path = Kohana::config('core.article_data_path');
		$content = @file_get_contents($article_path.$article_id.'.meta');
		if(!$content) return false;
		$meta = explode('|',$content);
		if(!empty($info['title']))
		{
			$meta[count($meta)-1] = str_replace('|','',$info['title']);
		}
		if(!empty($info['type']))
		{
			$meta[2] = $info['type'];
		}
		$meta = implode('|',$meta);
		file_put_contents($article_path.$article_id.'.meta',$meta);
	}

	public function publish($data)
	{
		$article_path = Kohana::config('core.article_data_path');
		($files = @glob($article_path."*.content")) OR $files = array();
		$article_id = count($files)+1;
		$content_name = $article_path.$article_id.'.content';
		file_put_contents($content_name,$data['content']);
		$meta_name = $article_path.$article_id.'.meta';
		$user = user::get_user();
		$meta = $article_id.'|'.$user->email.'|'.(int)$data['type'].'|0|0|'.time().'|'.str_replace("|","",$data['title']);
		file_put_contents($meta_name,$meta);
		return $article_id;
	}

	public function change_status($article_id,$status)
	{
		$article_path = Kohana::config('core.article_data_path');
		$meta_name = $article_path.$article_id.'.meta';
		$meta_info = explode('|',file_get_contents($meta_name));
		$meta_info[3] = $status;
		file_put_contents($meta_name,implode('|',$meta_info));
	}

}
